﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace homework3
{
    public class node<T>
    {
        public node<T> next { get; set; }
        public T data { get; set; }
        public node(T d) { data = d;next = null; }
    }
    public class List<T> where T : IComparable<T>
    {
        private node<T> head;
        private node<T> tail;
        public List()
        {
            head = tail = null;
        }
        public node<T> Head
        {
            get => head;
        }
        public void add(T data)
        {
            node<T> node = new node<T>(data);
            if (tail == null)
                head = tail = node;
            else
            {
                tail.next = node;
                tail = node;
            }
        }
        public void ForEach(Action<T> action)
        {
            node<T> current = head;
            while (current != null)
            {
                action(current.data);
                current = current.next;
            }
        }
        public void display() {
            ForEach(data => Console.Write(data + " "));
        }
        public T max() {
            if (head == null)
                throw new InvalidOperationException("链表为空。");

            T max = head.data;
            ForEach(data =>
            {
                if (data.CompareTo(max) > 0)
                    max = data;
            });
            return max;
        }
        public T min() {
            if (head == null)
                throw new InvalidOperationException("链表为空。");

            T min = head.data;
            ForEach(data =>
            {
                if (data.CompareTo(min) < 0)
                    min = data;
            });
            return min;
        }
        public T Sum()
        {
            if (head == null)
                throw new InvalidOperationException("链表为空。");

            dynamic sum = default(T); 
            ForEach(data => sum += data);
            return sum;
        }

    }
    internal class c4
    {
            static void Main(string[] args) { 
            List<int>list = new List<int>();
            list.add(1);
            list.add(2);
            list.add(3);
            list.display();
            Console.WriteLine("max" + list.max() + "  min:" + list.min()+"  sum:"+list.Sum());
        }

    }
}
